package com.kfm.jdbc.day04;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

public class DataSourceTest {

    public static void main(String[] args) throws Exception {
//        // 创建连接池对象
//        MyDataSource dataSource = new MyDataSource("com.mysql.jdbc.Driver",
//                "jdbc:mysql://localhost:3306/company", "root", "");
//
//        // 设置连接池的初始化大小和最大大小
//        dataSource.setInitSize(3);
//        dataSource.setMaxSize(10);
//
//        // 获取连接
//        try {
//            Connection connection = dataSource.getConnection();
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        }

        MyDataSource myDataSource = new MyDataSource();
        myDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        myDataSource.setUrl("jdbc:mysql://localhost:3306/company");
        myDataSource.setUsername("root");
        myDataSource.setPassword("");

//        System.out.println(myDataSource.getConnection());

//        Connection connection = myDataSource.getConnection();
//
//        System.out.println(connection);
//
//        connection.close();


        List<Connection> list = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            list.add(myDataSource.getConnection());
        }

        for (Connection connection : list) {
            System.out.println(connection);
            connection.close();
        }

        System.out.println("关闭连接后：");
        for (int i = 0; i < 10; i++) {
            System.out.println(myDataSource.getConnection());
        }

    }
}
